package com.google.code.sharecontent.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.google.code.sharecontent.dao.TypeDao;
import com.google.code.sharecontent.model.Type;

public class TypeDaoImpl extends BaseDaoImpl<Type> implements TypeDao {

	public TypeDaoImpl(Connection conn) {
		super(conn);
	}

	public Type getByCode(String code) throws SQLException {
		ArrayList<Type> list = this.list("code = '"+code+"'", "ID");
		if (list != null && list.size()>0) {
			return list.get(0);
		}
		return null;
	}
	
	@Override
	public Type createByRow(ResultSet rs) throws SQLException {
		Type status = new Type();
		status.setId(rs.getInt("ID"));
		status.setName(rs.getString("NAME"));
		status.setCode(rs.getString("CODE"));
		return status;
	}

	@Override
	protected String generateInsertSql(Type status) {
		return "insert into "+getTableName()+" (NAME, CODE) values (?,?)";
	}
	
	@Override
	protected String generateUpdateSql(Type status) {
		return "update "+getTableName()+" set NAME = ?,CODE = ? "+
		" where ID = "+ status.getId();
	}
	
	@Override
	protected String getTableName() {
		return "TYPES";
	}

	@Override
	protected PreparedStatement putPrepareStatement(PreparedStatement pStat,
			Type status) throws SQLException {
		pStat.setString(1, status.getName());
		pStat.setString(2, status.getCode());
		return pStat;
	}

}
